{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pandas 操作数据 输入输出\n",
    "# 1、读取文本文件和其他更高效的磁盘储存格式\n",
    "# 2、加载数据库中的数据\n",
    "# 3、利用 web API 操作网络资源\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>userId</th>\n",
       "      <th>movieId</th>\n",
       "      <th>rating</th>\n",
       "      <th>split_index</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2525823</th>\n",
       "      <td>16829</td>\n",
       "      <td>7371</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4128727</th>\n",
       "      <td>27171</td>\n",
       "      <td>43679</td>\n",
       "      <td>2.5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>908682</th>\n",
       "      <td>6114</td>\n",
       "      <td>4973</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5250989</th>\n",
       "      <td>34118</td>\n",
       "      <td>8798</td>\n",
       "      <td>4.5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9589939</th>\n",
       "      <td>62261</td>\n",
       "      <td>3160</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         userId  movieId  rating  split_index\n",
       "2525823   16829     7371     4.0            1\n",
       "4128727   27171    43679     2.5            1\n",
       "908682     6114     4973     4.0            1\n",
       "5250989   34118     8798     4.5            1\n",
       "9589939   62261     3160     1.0            1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1 文本文件\n",
    "# pandas 提供方法 将数据读取为 DataFrame 对象\n",
    "# 函数          说明\n",
    "# read_csv      从文件、URL、文件型对象中加载带分隔符的数据，默认分隔符为逗号\n",
    "# read_table    从文件、URL、文件型对象中加载带分隔符的数据，默认分隔符为“\\t”\n",
    "# read_json     读取 JSON 字符串中的数据\n",
    "# read_excel    从 Excel XLS 或者 XLSX 文件类型中读取表格数据\n",
    "# read_hdf      读取 pandas 写的 HDFS 文件\n",
    "# read_html     读取 HTML 文档中的所有表格\n",
    "# read_msgpack  二进制格式编码的 pandas 数据\n",
    "# read_pickle   读取 Python pickle格式中储存的任意对象\n",
    "# read_sas      读取储存于 SAS 系统自定义储存格式的 SAS 数据集\n",
    "# read_sql      读取 SQL 查询结果为 pandas 的 DataFrame\n",
    "# read_stata    读取 Stata 文件格式的数据集\n",
    "# read_feather  读取 Feather 二进制文件格式\n",
    "\n",
    "# the path of dataset\n",
    "dataset_path = './../dataset/'\n",
    "\n",
    "df = pd.read_csv(dataset_path + 'test.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>userId</th>\n",
       "      <th>movieId</th>\n",
       "      <th>rating</th>\n",
       "      <th>split_index</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>21533729</th>\n",
       "      <td>139994</td>\n",
       "      <td>161</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12995078</th>\n",
       "      <td>84148</td>\n",
       "      <td>45</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10190611</th>\n",
       "      <td>66078</td>\n",
       "      <td>1199</td>\n",
       "      <td>4.5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68283</th>\n",
       "      <td>546</td>\n",
       "      <td>5618</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12751939</th>\n",
       "      <td>82461</td>\n",
       "      <td>1580</td>\n",
       "      <td>4.5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          userId  movieId  rating  split_index\n",
       "21533729  139994      161     3.0            1\n",
       "12995078   84148       45     4.0            1\n",
       "10190611   66078     1199     4.5            1\n",
       "68283        546     5618     5.0            1\n",
       "12751939   82461     1580     4.5            1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.read_table(dataset_path + 'test.csv', sep=',')\n",
    "df2.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2525823</td>\n",
       "      <td>16829</td>\n",
       "      <td>7371</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4128727</td>\n",
       "      <td>27171</td>\n",
       "      <td>43679</td>\n",
       "      <td>2.5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>908682</td>\n",
       "      <td>6114</td>\n",
       "      <td>4973</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5250989</td>\n",
       "      <td>34118</td>\n",
       "      <td>8798</td>\n",
       "      <td>4.5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>9589939</td>\n",
       "      <td>62261</td>\n",
       "      <td>3160</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         0      1      2    3  4\n",
       "0  2525823  16829   7371  4.0  1\n",
       "1  4128727  27171  43679  2.5  1\n",
       "2   908682   6114   4973  4.0  1\n",
       "3  5250989  34118   8798  4.5  1\n",
       "4  9589939  62261   3160  1.0  1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 没有标题行\n",
    "# pandas 默认分配列名\n",
    "pd.read_csv(dataset_path + 'test-copy.csv', header=None).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>userID</th>\n",
       "      <th>movieID</th>\n",
       "      <th>rating</th>\n",
       "      <th>split</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>21533729</th>\n",
       "      <td>139994</td>\n",
       "      <td>161</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12995078</th>\n",
       "      <td>84148</td>\n",
       "      <td>45</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10190611</th>\n",
       "      <td>66078</td>\n",
       "      <td>1199</td>\n",
       "      <td>4.5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>68283</th>\n",
       "      <td>546</td>\n",
       "      <td>5618</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12751939</th>\n",
       "      <td>82461</td>\n",
       "      <td>1580</td>\n",
       "      <td>4.5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          userID  movieID  rating  split\n",
       "21533729  139994      161     3.0      1\n",
       "12995078   84148       45     4.0      1\n",
       "10190611   66078     1199     4.5      1\n",
       "68283        546     5618     5.0      1\n",
       "12751939   82461     1580     4.5      1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 自定义列名\n",
    "pd.read_csv(dataset_path + 'test-copy.csv', names=['userID', 'movieID', 'rating', 'split']).tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 有些情况下，数据间分隔符不是固定的 \n",
    "# 这需要对数据集进行规整，常传入 正则表达式 作为 read_table 的分隔符\n",
    "# 解析器函数有许多参数帮助解决处理各种类型的异形文件格式\n",
    "#     - skiprows 跳过文件的行\n",
    "# 缺失值 ： 空字符串、NA、NULL\n",
    "#     - na_values 一组替换NA的值\n",
    "\n",
    "# read_csv/read_table 函数的参数\n",
    "# 参数          说明\n",
    "# path          文件系统位置，URL，文件型对象的字符串\n",
    "# sep/delimiter 行的字段进行拆分的字符序列或者正则表达式\n",
    "# date_parser   用于解析日期的函数\n",
    "# nrows         指定读取的行数\n",
    "# chunksize     文件块的大小，用于迭代\n",
    "# encoding      用于 Unicode 的文本编码格式，如 utf-8\n",
    "# thousands     千分位分隔符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 文件写入\n",
    "# 将一个 DataFrame 对象写入文件，to_csv 方法，可以指定分隔符\n",
    "# 将一个 DataFrame 对象写入文件，to_json 方法，\n",
    "\n",
    "# 处理分隔符格式\n",
    "# Python 内置模块 csv\n",
    "# import csv\n",
    "\n",
    "# JSON数据\n",
    "# Python 内置模块 json\n",
    "# json.loads 将 JSON 字符串转换为 Python形式\n",
    "# json.dumps 将 Python对象转换为 JSON 格式\n",
    "# import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2 二进制数据格式\n",
    "\n",
    "# 实现数据的高效二进制储存\n",
    "# 使用 Python 内置 pickle 序列化\n",
    "# pandas 对象都有一个用于将数据以 pickle 格式保存到磁盘方法 to_pickle\n",
    "# 可以通过 pickle 直接读取被pickle化的数据，或者使用 pandas.read_pickle\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<class 'pandas.io.pytables.HDFStore'>\n",
       "File path: mydata.h5"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# HDF5 格式\n",
    "\n",
    "# HDF5是一种存储大规模科学数组数据的非常好的文件格式。\n",
    "#     它可以被作为C标准库， 带有许多语言的接口， 如Java、 Python和MATLAB等。 \n",
    "# HDF5中的 HDF 指的是层次型数据格式（hierarchicaldata format） \n",
    "\n",
    "# 每个HDF5文件都含有一个文件系统式的节点结构， 它使你能够存储多个数据集并支持元数据。 \n",
    "# 与其他简单格式相比， HDF5支持多种压缩器的即时压缩， 还能更高效地存储重复模式数据。 \n",
    "# 对于那些非常大的无法直接放入内存的数据集， HDF5就是不错的选择， 因为它可以高效地分块读写。\n",
    "# 可以用 PyTables 或 h5py 库直接访问 HDF5 文件， \n",
    "# pandas提供了更为高级的接口，简化存储Series和DataFrame对象。 HDFStore类可以像字典一样， 处理低级的细节：\n",
    "frame = pd.DataFrame({'a': np.random.randn(100)})\n",
    "store = pd.HDFStore(dataset_path + 'mydata.h5')\n",
    "store['obj1'] = frame\n",
    "store['obj1_col'] = frame['a']\n",
    "store\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.747949</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.674777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.068323</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2.117886</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.716841</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>0.561240</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>-0.981324</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>-0.489343</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>0.075235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>0.413663</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           a\n",
       "0   0.747949\n",
       "1  -0.674777\n",
       "2   0.068323\n",
       "3  -2.117886\n",
       "4  -0.716841\n",
       "..       ...\n",
       "95  0.561240\n",
       "96 -0.981324\n",
       "97 -0.489343\n",
       "98  0.075235\n",
       "99  0.413663\n",
       "\n",
       "[100 rows x 1 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# HDF5文件中的对象可以通过与字典一样的API进行获取：\n",
    "store['obj1']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# HDFStore支持两种存储模式， 'fixed'和'table'。 \n",
    "# 后者通常会更慢， 但是支持使用特殊语法进行查询操作：\n",
    "store.put('obj2', frame, format='table')\n",
    "store.select('obj2', where=['index >= 10 and index <= 15'])\n",
    "store.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.747949</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.674777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.068323</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2.117886</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.716841</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          a\n",
       "0  0.747949\n",
       "1 -0.674777\n",
       "2  0.068323\n",
       "3 -2.117886\n",
       "4 -0.716841"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# put是store['obj2'] = frame方法的显示版本， 允许我们设置其它的选项， 比如格式。\n",
    "# pandas.read_hdf函数可以快捷使用这些工具：\n",
    "frame.to_hdf(dataset_path + 'mydata.h5', 'obj3', format='table')\n",
    "pd.read_hdf(dataset_path + 'mydata.h5', 'obj3', where=['index < 5'])\n",
    "\n",
    "# 笔记： 如果你要处理的数据位于远程服务器， 比如Amazon S3或HDFS， \n",
    "# 使用专门为分布式存储（比如Apache Parquet） 的二进制格式也许更加合适。 \n",
    "# Python的 Parquet和其它存储格式还在不断的发展之中\n",
    "\n",
    "# 如果需要本地处理海量数据， 建议研究一下 PyTables和 h5py， 看看它们能满足你的哪些需求\n",
    "# 由于许多数据分析问题都是IO密集型（而不是CPU密集型），利用 HDF5 这样的工具能显著提升应用程序的效率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Web APIs 交互数据\n",
    "\n",
    "# 许多网站都有一些通过 JSON 或其他格式提供数据的公共 API。 \n",
    "# 通过Python访问这些API的办法有不少。 \n",
    "# 一个简单易用的办法（推荐） 是requests包（http://docs.python-requests.org） 。\n",
    "# 为了搜索最新的30个GitHub上的pandas主题，发一个HTTP GET请求， 使用requests扩展库：\n",
    "\n",
    "import requests\n",
    "\n",
    "url = 'https://lab.isaaclin.cn/nCoV/api/overall'\n",
    "\n",
    "resp = requests.get(url)\n",
    "resp\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'results': [{'currentConfirmedCount': 441,\n",
       "   'currentConfirmedIncr': 39,\n",
       "   'confirmedCount': 85070,\n",
       "   'confirmedIncr': 52,\n",
       "   'suspectedCount': 1885,\n",
       "   'suspectedIncr': 9,\n",
       "   'curedCount': 79983,\n",
       "   'curedIncr': 13,\n",
       "   'deadCount': 4646,\n",
       "   'deadIncr': 0,\n",
       "   'seriousCount': 99,\n",
       "   'seriousIncr': -15,\n",
       "   'globalStatistics': {'currentConfirmedCount': 4304709,\n",
       "    'confirmedCount': 9073850,\n",
       "    'curedCount': 4297182,\n",
       "    'deadCount': 471959,\n",
       "    'currentConfirmedIncr': -20905,\n",
       "    'confirmedIncr': 40655,\n",
       "    'curedIncr': 60088,\n",
       "    'deadIncr': 1472},\n",
       "   'generalRemark': '1. 3 月 12 日国家卫健委确诊补订遗漏 12 例确诊病例（非 12 日新增），暂无具体省份信息。 2. 浙江省 12 例外省治愈暂无具体省份信息。',\n",
       "   'remark1': '易感人群：人群普遍易感。老年人及有基础疾病者感染后病情较重，儿童及婴幼儿也有发病',\n",
       "   'remark2': '潜伏期：一般为 3～7 天，最长不超过 14 天，潜伏期内可能存在传染性，其中无症状病例传染性非常罕见',\n",
       "   'remark3': '宿主：野生动物，可能为中华菊头蝠',\n",
       "   'remark4': '',\n",
       "   'remark5': '',\n",
       "   'note1': '病毒：SARS-CoV-2，其导致疾病命名 COVID-19',\n",
       "   'note2': '传染源：新冠肺炎的患者。无症状感染者也可能成为传染源。',\n",
       "   'note3': '传播途径：经呼吸道飞沫、接触传播是主要的传播途径。气溶胶传播和消化道等传播途径尚待明确。',\n",
       "   'updateTime': 1592903342750}],\n",
       " 'success': True}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 响应对象的json方法会返回一个包含被解析过的JSON字典， 加载到一个Python 对象中\n",
    "data = resp.json()\n",
    "\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4 数据库交互\n",
    "\n",
    "# 在商业场景下， 大多数数据可能不是存储在文本或Excel文件中。 \n",
    "# 基于SQL的关系型数据库（如SQL Server、 PostgreSQL和MySQL等）使用非常广泛。 \n",
    "# 数据库的选择通常取决于性能、 数据完整性以及应用程序的伸缩性需求。\n",
    "\n",
    "# 将数据从SQL加载到DataFrame的过程很简单， 此外pandas还有一些能够简化该过程的函数\n",
    "\n",
    "# 例如，使用 SQLite 数据库，通过 Python 内置的 sqlite3 驱动器\n",
    "import sqlite3\n",
    "\n",
    "# 从表中选取数据时， 大部分Python SQL驱动器（PyODBC、 psycopg2、 MySQLdb、 pymssql等）\n",
    "#   都会返回一个元组列表：\n",
    "# 将这个元组列表传给DataFrame构造器， 但还需要列名（位于光标的description属性中） \n",
    "\n",
    "# 这种数据规整操作相当多， 你肯定不想每查一次数据库就重写一次。 \n",
    "# SQLAlchemy项目是一个流行的Python SQL工具，它抽象出了SQL数据库中的许多常见差异。 \n",
    "# pandas有一个 read_sql 函数， 可以让你轻松的从 SQLAlchemy 连接读取数据\n",
    "\n",
    "# 用 SQLAlchemy 连接 SQLite 数据库， 并读取数据\n",
    "# \n",
    "# import sqlalchemy as sqla\n",
    "# db = sqla.create_engine('sqlite:///mydata.sqlite')\n",
    "# pd.read_sql('select * from test', db)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 访问到自己需要的数据，this is the first step\n",
    "# 研究 数据规整、数据可视化、时间序列分析\n",
    "# 在数据分析和建模过程中，数据准备：加载、清理、转换、重塑\n",
    "# 这些流程占据分析师大部分时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
